package com.dw.dwdzcommerce.base.activity;

import com.dw.dwdzcommerce.tools.commons.viewUtil.ViewUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;


public abstract class BaseActivity extends FragmentActivity {
    public static final String TAG = "baseActivity";
    protected static final int TURN_TO_LOGIN = -20;

    boolean setFlag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setFlag = isSetFlag();
        beforeSetContent();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && setFlag) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
            beforeInCreate(savedInstanceState);
            inCreate(savedInstanceState);
        }
    }

    protected void beforeInCreate(Bundle savedInstanceState) {

    }

    protected void beforeSetContent() {

    }

    protected boolean isSetFlag() {

        return false;
    }

    public void setSetFlag(boolean setFlag) {
        this.setFlag = setFlag;
    }

    protected Intent getDefaultIntent(Context context, Class<?> clazz) {
        return new Intent(context, clazz);
    }

    protected void startActivity(String key, String value, Bundle bundle, Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        if (bundle == null) {
            intent.putExtra(key, value);
        } else {
            bundle.putString(key, value);
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void log(String text) {
        Log.i(TAG, text);
    }

    protected abstract void inCreate(Bundle savedInstanceState);

    protected abstract int getLayoutId();

    @SuppressWarnings("unchecked")
    protected <T> T findView(int id) {
        T view = (T) findViewById(id);
        return view;
    }

    public void toast(Context context, String text) {
        ViewUtil.showSingleToastShort(context, text);
    }

    public void dismissSoftInput(Activity mContext) {
        // 闅愯棌杞敭鐩�
        InputMethodManager imm = ((InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE));
        if (mContext.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (mContext.getCurrentFocus() != null)
                imm.hideSoftInputFromWindow(mContext.getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public void dismissSoftInput() {
        // 闅愯棌杞敭鐩�
        dismissSoftInput(this);
    }


//    public  Handler mTransferHandler = new Handler() {
//        public void handleMessage(android.os.Message msg) {
//            switch (msg.what) {
//                case TURN_TO_LOGIN:
//                    Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
//                    startActivity(intent);
//                    break;
//                default:
//                    break;
//            }
//        };
//    };


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    public void showAndHideFragment(int containerRes, Fragment showFragment, Fragment hideFragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (!hideFragment.isAdded()) {
            fragmentTransaction.add(containerRes, hideFragment);
        }
        fragmentTransaction.hide(hideFragment);
        if (!showFragment.isAdded()) {
            fragmentTransaction.add(containerRes, showFragment);
        }
        fragmentTransaction.show(showFragment);
        fragmentTransaction.commit();
    }

    public void hideFragment(Fragment fragment) {
        if(!fragment.isAdded()){
            return;
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.hide(fragment);
    }

    public void launch(Class<?> clazz, View transitionView, String url, String shareName) {
        Intent intent = getClassIntent(clazz);
        intent.putExtra("url", url);
       
        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, transitionView, shareName);
        ActivityCompat.startActivity(this, intent, options.toBundle());
    }


    protected Intent getClassIntent(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        return intent;
    }


}
